package com.hepinghospital.user.ui.activity.userinfo;

import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hepinghospital.user.R;
import com.hepinghospital.user.base.BaseActivity;
import com.hepinghospital.user.ui.dialog.AreaDialogFragment;
import com.hepinghospital.user.ui.dialog.ConfirmDialog;
import com.hepinghospital.user.util.ToastUtil;
import com.rest.business.RestProxy;
import com.rest.observer.BaseObserver;
import com.rest.response.AddressDetailResponse;
import com.rest.response.AddressResponse;

import butterknife.BindView;
import butterknife.OnClick;
import jz.joyoung.robot.util.UtilString;

/**
 * 添加地址/编辑地址
 * Created by HP on 2020/2/20.
 */

public class AddAddressActivity extends BaseActivity {
    @BindView(R.id.edt_name)
    EditText edt_name;
    @BindView(R.id.edt_area)
    TextView edt_area;
    @BindView(R.id.edt_area_detail)
    EditText edt_area_detail;
    @BindView(R.id.edt_phone)
    EditText edt_phone;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.btn_confirm)
    TextView btnConfirm;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.btn_delete)
    ImageView btnDelete;

    private AreaDialogFragment mAreaDialogFragment;
    String ids, provinceId, cityId, regionId, isDefault = "0";
    private AddressResponse address;
    private String addressId = "";

    @Override
    protected void initViews() {
        btnDelete.setOnClickListener(v -> { showConfirmDialog();});
    }

    @Override
    protected void initData() {
        address = (AddressResponse) getIntent().getSerializableExtra("obj");
        if (address != null) {
            btnDelete.setVisibility(View.VISIBLE);
            btnConfirm.setText("保 存");
            tvTitle.setText("编辑地址");
            addressId = address.id;
            getAddressDetail();
        } else {
            btnConfirm.setText("确 定");
            btnDelete.setVisibility(View.GONE);
            tvTitle.setText("添加地址");
        }
    }

    @OnClick(R.id.btn_confirm)
    void btn_confirm() {
        addFamily();
    }

    @OnClick(R.id.edt_area)
    void edt_area() {
        showAreaDialog();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_add_address;
    }


    private void addFamily() {
        if (UtilString.isEmpty(edt_name.getText().toString().trim())) {
            ToastUtil.showToast(mContext, "姓名不能为空");
            return;
        }
        if (UtilString.isEmpty(edt_area.getText().toString().trim())) {
            ToastUtil.showToast(mContext, "地区不能为空");
            return;
        }
        if (UtilString.isEmpty(edt_area_detail.getText().toString().trim())) {
            ToastUtil.showToast(mContext, "详细地址不能为空");
            return;
        }
        if (UtilString.isEmpty(edt_phone.getText().toString().trim())) {
            ToastUtil.showToast(mContext, "手机号不能为空");
            return;
        } else {
            if (!UtilString.isPhoneNumNo(edt_phone.getText().toString().trim())) {
                ToastUtil.showToast(mContext, "请输入正确的手机号");
                return;
            }
        }

        if (checkbox.isChecked()) {
            isDefault = "1";
        } else {
            isDefault = "0";
        }
        updateAddress("1");
    }

    /**
     * 是否删除 0删除  1正常
     *
     * @param isDelete
     */
    private void updateAddress(String isDelete) {
        try {
            RestProxy.getInstance().addAddress(addressId, edt_name.getText().toString(), edt_phone.getText().toString(), edt_area_detail.getText().toString(), isDefault, provinceId, cityId, regionId, isDelete, new BaseObserver<String>() {
                @Override
                public void onError(Throwable e) {
                    e.printStackTrace();
                    ToastUtil.onError(AddAddressActivity.this, e);
                }

                @Override
                public void onSuccess(String result) {
                    finish();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void showConfirmDialog() {
        ConfirmDialog confirmDialog = ConfirmDialog.newInstance("确认删除该收获地址？","");
        confirmDialog.show(getSupportFragmentManager(), "ConfirmDialog");
        confirmDialog.setmListener(new ConfirmDialog.OnItemClickListener() {
            @Override
            public void onConfirm() {
                updateAddress("0");
            }

            @Override
            public void onCancel() {

            }
        });
    }

    private void showAreaDialog() {
        if (mAreaDialogFragment == null) {
            mAreaDialogFragment = AreaDialogFragment.newInstance();
            mAreaDialogFragment.show(getSupportFragmentManager(), "mAreaDialogFragment");
            mAreaDialogFragment.setmListener(new AreaDialogFragment.OnItemClickListener() {
                @Override
                public void onConfirm(String ids, String address) {
                    AddAddressActivity.this.ids = ids;
                    provinceId = ids.split(",")[0];
                    cityId = ids.split(",")[1];
                    regionId = ids.split(",")[2];
                    edt_area.setText(address);
                }
            });
        } else if (!mAreaDialogFragment.isAdded() && !mAreaDialogFragment.isShowing()) {
            mAreaDialogFragment.show(getSupportFragmentManager(), "mAreaDialogFragment");
        }
    }

    /**
     * 获取地址详情
     *
     * @time 2020/4/16 14:10
     */
    private void getAddressDetail() {
        RestProxy.getInstance().getAddressDetail(addressId, new BaseObserver<AddressDetailResponse>() {
            @Override
            public void onSuccess(AddressDetailResponse addressResponse) {
                edt_name.setText(addressResponse.consigneeName);
                edt_phone.setText(addressResponse.consigneePhone);
                edt_area.setText(addressResponse.area);
                edt_area_detail.setText(addressResponse.addr);
                provinceId = addressResponse.province;
                cityId = addressResponse.city;
                regionId = addressResponse.region;
                if ("0".equals(addressResponse.isDefault)) {
                    checkbox.setChecked(false);
                } else if ("1".equals(addressResponse.isDefault)) {
                    checkbox.setChecked(true);
                }
            }
        });
    }
}
